
/**
 * 生成随机字符串
 * @param {number} len 字符串长度
 * @return {string}
 */
 export function randomString(len:number) {
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    var maxPos = $chars.length;
    var pwd = '';
    for (let i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

/**深拷贝 */
export function deepClone (obj:any, hash = new WeakMap()) {
	if (obj === null || typeof obj !== 'object') {
		return obj
	}
	if (hash.has(obj)) {
		return hash.get(obj)
	}
	let cloneObj:any
	if (obj instanceof Array) {
		cloneObj = []
	} else {
		cloneObj = {}
	}
	hash.set(obj, cloneObj)
	for (const key in obj) {
		if (obj.hasOwnProperty(key)) {
			cloneObj[key] = deepClone(obj[key], hash)
		}
	}
	return cloneObj
}
/**base64 转 blob */
export function dataURLtoBlob(dataurl:string) {
    let arr = dataurl.split(',');
	if(arr&&arr[0]){
		let str=arr[0]
		let mime = str!.match(/:(.*?);/)![1];
		let bstr = window.atob(arr[1]);
		let n = bstr.length;
		let u8arr = new Uint8Array(n);
		while (n--) {
			u8arr[n] = bstr.charCodeAt(n);
		}
		return new Blob([u8arr], { type: mime });
	}else{
		return null
	}

}
/**base64 转 file */
export function dataURLtoFile(dataurl) {
	
	var arr = dataurl.split(','),
		mime = arr[0].match(/:(.*?);/)[1],
		bstr = atob(arr[1]), 
		n = bstr.length, 
		u8arr = new Uint8Array(n);
		
	while(n--){
		u8arr[n] = bstr.charCodeAt(n);
	}
	var filename=randomString(32)+'.'+(mime.split('/')[1])
	return new File([u8arr], filename, {type:mime});
}
/**像素转矩阵 */
export function pixel2Matrix(array:any[],width:number){
	let newArr=sliceIntoChunks(array,4)
	newArr=sliceIntoChunks(newArr,width)
    return newArr
}

const sliceIntoChunks=(arr:any[],chunkSize:number)=>{
    const res:any[] = [];
    for (let i = 0; i < arr.length; i += chunkSize) {
        const chunk = arr.slice(i, i + chunkSize);
        res.push(chunk);
    }
    return res;
}


export function getConstraintSize(width:number,height:number,maxWidth:number,maxHeight:number):{width:number,height:number}{
	var ratio=width/height;
	if(width<=maxWidth&&height<=maxHeight){
		return {
			width,height
		}
	}
	width=maxWidth;
	height=width/ratio;
	if(height>maxHeight){
		height=maxHeight;
		width=height*ratio;
	}
	return {width,height}
}
//时间格式化
export function dateFormat(d, fmt = 'yyyy-MM-dd hh:mm:ss') {
	if (typeof d == 'string') {
		if (d.length == 10) d = d + '000'
		d = Number(d)
	}
	if (typeof d == 'number') {
		if (d.toString().length == 10) d = d * 1000
		d = new Date(d)
	}
	var o = {
		"M+": d.getMonth() + 1, //月份
		"d+": d.getDate(), //日
		"h+": d.getHours(), //小时
		"m+": d.getMinutes(), //分
		"s+": d.getSeconds(), //秒
		"q+": Math.floor((d.getMonth() + 3) / 3), //季度
		"S": d.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}